﻿using Easyliter;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace 股票数据
{
    public class Utils
    {

        private static bool path_checked = false;
        private static string path = "";
        public static string DB_PATH {
            get
            {
                lock(path)
                {
                    if (!path_checked)
                    {
                        string t_path = @"database\";
                        if (!Directory.Exists(path))
                        {
                            Directory.CreateDirectory(t_path);
                        }
                        string file = t_path + "stock.db";
                        //if (!File.Exists(file))
                        //{
                        //    File.Create(file);
                        //}
                        path = new FileInfo(file).FullName;
                        path = path.Replace("\\", "//");

                        return path;
                    }
                    else
                    {
                        return path;
                    }
                }
            }
        }


        private static string reports_path = null;
        public static string REPORT_PATH
        {
            get
            {
                if (reports_path == null)
                {
                    string t_path = @"database\reports\";
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(t_path);
                    }
                    reports_path = new DirectoryInfo(t_path).FullName;
                    return reports_path;
                }
                else
                {
                    return reports_path;
                }
            }
        }



        private static Client client = null;
        private static object cl_mutex = new object();
        public static Client DB_Client
        {
            get{
                lock(cl_mutex)
                {
                    if (client == null)
                    {
                        client = new Client("DataSource=" + DB_PATH);
                        return client;
                    }
                    return client;
                }
            }
        }



        public static String ConvertJs2Csharp(String js)
        {
            js = js.Replace("&quot;", "＂");
            js = js.Replace("&amp;", "&");
            js = js.Replace("&lt;", "<");
            js = js.Replace("&gt;", ">");
            js = js.Replace("&nbsp;", " ");

            StringBuilder sb = new StringBuilder();
            StringBuilder temp = new StringBuilder(js);

            while (temp.Length > 0)
            {
                Regex r = new Regex(@"^\\u([0-9a-f]{4}).*");
                var match = r.Match(temp.ToString());
                if (match.Success)
                {
                    sb.Append((char)Convert.ToInt32(match.Groups[1].Value, 16));
                    temp.Remove(0, 6);
                }
                else
                {
                    r = new Regex(@"^\\/.*");
                    match = r.Match(temp.ToString());
                    if (match.Success)
                    {
                        sb.Append('/');
                        temp.Remove(0, 2);
                    }
                    else
                    {
                        sb.Append(temp[0]);
                        temp.Remove(0, 1);
                    }
                }
            }
            return sb.ToString();
        }

    }
}
